package com.njust.diary.service;

import cn.hutool.core.util.StrUtil;
import com.njust.diary.dao.NoteDao;
import com.njust.diary.dao.NoteTypeDao;
import com.njust.diary.po.Comment;
import com.njust.diary.po.Like;
import com.njust.diary.po.Note;
import com.njust.diary.po.NoteType;
import com.njust.diary.util.Page;
import com.njust.diary.vo.NoteVo;
import com.njust.diary.vo.Result;
import com.sun.org.apache.bcel.internal.generic.NEW;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NoteService {
    private NoteDao noteDao =new NoteDao();

    public Result<Note> addOrUpdate(String typeId, String title, String content,String noteId,String lon,String lat,String uname) {
   Result<Note> result =new Result<>();
        if(StrUtil.isBlank(typeId)){
            result.setCode(0);
            result.setMsg("请选择云记类型");
              return  result;
        }
        if(StrUtil.isBlank(title)){
            result.setCode(0);
            result.setMsg("云记标题不能为空");
            return  result;
        }
        if(StrUtil.isBlank(content)){
            result.setCode(0);
            result.setMsg("云记内容不能为空");
            return  result;
        }


        if(StrUtil.isBlank(lon)||StrUtil.isBlank(lat)){
            lon="116.404";
            lat="39.915";
        }

   Note note =new Note();
   note.setTitle(title);
   note.setContent(content);
   note.setTypeId(Integer.parseInt(typeId));
   note.setLon(Float.parseFloat(lon));
        note.setLat(Float.parseFloat(lat));
        note.setUname(uname);
   if(!StrUtil.isBlank(noteId)){
       note.setNoteId(Integer.parseInt(noteId));
   }



 result.setResult(note);

 int row =noteDao.addOrUpdate(note);
if(row>0){
    result.setCode(1);
}else{
    result.setCode(0);
    result.setResult(note);
}

   return  result;
    }

    public Page<Note> findNoteListByPage(String pageNumStr,  Integer userId,String title,String date,String type) {
           Integer pageNum =1;
           Integer pageSize=10;//默认值
          if(!StrUtil.isBlank(pageNumStr)){
              pageNum=Integer.parseInt(pageNumStr);
          }

        long count =noteDao.findNoteCount(userId,title,date,type);
        if(count<1){
            return null;
        }
        Page<Note> page = new Page<>(pageNum,pageSize,count);
        Integer index =(pageNum-1)*pageSize;

        List<Note> noteList = noteDao.findNoteListByPage(userId,index,pageSize,title,date,type);

        page.setDataList(noteList);

return page;
    }

    public List<NoteVo> findNoteCountByDate(Integer userId) {
        return noteDao.findNoteCountByDate(userId);


    }

    public List<NoteVo> findNoteCountByType(Integer userId) {
        return noteDao.findNoteCountByType(userId);
    }

    public Note findNoteById(String noteId) {
        if(StrUtil.isBlank(noteId)){
            return null;
        }
        Note note =noteDao.findNoteById(noteId);
        return note;


    }

    public Integer deleteNote(String noteId) {
   if(StrUtil.isBlank(noteId)){
       return 0;
   }
   int row =noteDao.deleteNoteById(noteId);
   if(row>0){
       return 1;
   }
   return 0;


    }

    public Result<Map<String, Object>> queryNoteCountByMonth(Integer userId) {
        Result<Map<String, Object>> result = new Result<>();
        List<NoteVo> noteVos = noteDao.findNoteCountByDate(userId);
        if(noteVos!=null && noteVos.size()>0){
            List<String> monthList = new ArrayList<>();
            List<Integer> noteCountList = new ArrayList<>();
            for(NoteVo noteVo: noteVos){
                monthList.add(noteVo.getGroupName());
                noteCountList.add((int) noteVo.getNoteCount());
            }
            Map<String,Object> map =new HashMap<>();
            map.put("monthArray",monthList);
            map.put("dataArray",noteCountList);
              result.setCode(1);
              result.setResult(map);
        }

        return  result;



    }

    public Result<List<Note>> queryNoteLocation(Integer userId) {
        Result<List<Note>> result =new Result<>();
        List<Note> noteList =noteDao.queryNoteList(userId);

        if(noteList!=null && noteList.size()>0){
            result.setCode(1);
            result.setResult(noteList);
        }
        return  result;



    }



//        public List<NoteType> findTypeList(Integer userId){
//            List<NoteType> typeList =typeDao.findTypeListByUserId(userId);
//            return  typeList;
//        }
    public List<Comment> findComList(String noteId) {
        List<Comment> commentList = noteDao.findCommentListByNoteId(noteId);
        return commentList;
    }


    public Result<Comment> addComment(String noteId, String content, String uname) {
        Result<Comment> result =new Result<>();
        if(StrUtil.isBlank(content)){
            result.setCode(0);
            result.setMsg("评论内容不能为空");
            return  result;
        }
        Comment comment = new Comment();
        comment.setNoteId(Integer.valueOf(noteId));
        comment.setContent(content);
        comment.setUname(uname);
        result.setResult(comment);

        int row =noteDao.addComment(comment);
        if(row>0){
            result.setCode(1);
        }else{
            result.setCode(0);
            result.setResult(comment);
        }

        return  result;


    }

    public Integer findLikebyUserId(Integer userId) {
        Like like = new Like();
                like=noteDao.findLikebyUserId(userId);
        if(like!=null){
            return 1;
        }else{
            return  0;
        }

    }

    public Long findLikebyNoteId(String noteId) {

        Long likescount= Long.valueOf(0);
        likescount=noteDao.findlikescountByNoteId(noteId);
        return likescount;
    }

    public void deleteLike(Integer userId, Integer noteId2) {

        noteDao.deleteLikeById(noteId2,userId);


    }

    public void insertLike(Integer userId, Integer noteId2) {
        noteDao.insertLikeById(noteId2,userId);
    }
}
